home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / V15N04.ZIP / WARPCA.ZIP / WCABSRC.ZIP / FILEASOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  3.5 KB  |  159 lines

  1. // FILEASOC.CPP -- Class implementation to manage file types and associations.
  2. #define INCL_WINSHELLDATA
  3. #include <os2.h>
  4. #include <owl\owlpch.h>
  5. #include <owl\applicat.h>
  6. #include <cstring.h>
  7. #include "resource.h"
  8. #include "fileasoc.h"
  9.  
  10. int FindFileAssociation(const FILEASSOCIATION& ThisRec, void *pMatchExt)
  11. {
  12.     LPSTR lpszMatchExt = (LPSTR) pMatchExt;
  13.  
  14.     if(strcmpi(ThisRec.szExt, lpszMatchExt) == 0)
  15.         return TRUE;
  16.     else
  17.         return FALSE;
  18. }
  19.  
  20. FILEASSOCIATION::FILEASSOCIATION(LPSTR lpszMyFileExt, LPSTR lpszMyDesc, LPSTR lpszMyEXEPath, int nMyImage)
  21. {
  22.     if(!lpszMyFileExt)
  23.         strcpy(szExt,"");
  24.     else
  25.         strcpy(szExt, lpszMyFileExt);
  26.  
  27.     if(!lpszMyDesc)
  28.         strcpy(szDesc, "N.A.");
  29.     else
  30.         strcpy(szDesc, lpszMyDesc);
  31.  
  32.     if(!lpszMyEXEPath)
  33.         strcpy(szEXEPathName, "");
  34.     else
  35.         strcpy(szEXEPathName, lpszMyEXEPath);
  36.  
  37.     iImage = nMyImage;
  38. }
  39.  
  40. FILEASSOCIATION::FILEASSOCIATION(const FILEASSOCIATION& OldAssoc)
  41. {
  42.     // Copy data from source object.
  43.     strcpy(szExt, OldAssoc.szExt);
  44.     strcpy(szDesc, OldAssoc.szDesc);
  45.     strcpy(szEXEPathName, OldAssoc.szEXEPathName);
  46.     iImage = OldAssoc.iImage;   // Image used to represent this file type in listbox.
  47. }
  48.  
  49. BOOL FILEASSOCIATION::SaveToProfileFile(HWND hwnd, LPSTR lpszPRFFilename, LPSTR lpszSection, int nIndex)
  50. {
  51.     // OS/2 Version uses profile functions.
  52.     string strKeyword;
  53.  
  54.     strKeyword = "Type";
  55.  
  56.     // E.g. Type1 - Type20, etc.
  57.     char szTemp[20];
  58.     itoa(nIndex, szTemp, 10);
  59.     strKeyword += szTemp;
  60.  
  61.     string strData;
  62.  
  63.     // Save this record as a series of comma-delimited fields.
  64.     strData = szExt;
  65.     strData += ",";
  66.     strData += szDesc;
  67.     strData += ",";
  68.     strData += szEXEPathName;
  69.     strData += ",";
  70.  
  71.     itoa(iImage, szTemp, 10);
  72.     strData += szTemp;
  73.  
  74.     HINI hini = PrfOpenProfile(WinQueryAnchorBlock(hwnd), lpszPRFFilename);
  75.  
  76.     if(hini == NULLHANDLE)
  77.         return FALSE;
  78.  
  79.     int rc = PrfWriteProfileString(hini, lpszSection, strKeyword.c_str(), strData.c_str());
  80.  
  81.     // (For Windows!) int rc = WritePrivateProfileString(lpszSection, strKeyword.c_str(), strData.c_str(), lpszPRFFilename);
  82.  
  83.     PrfCloseProfile(hini);
  84.  
  85.     return rc;
  86. }
  87.  
  88. BOOL FILEASSOCIATION::LoadFromProfileFile(HWND hwnd, LPSTR lpszPRFFilename, LPSTR lpszSection, int nIndex)
  89. {
  90.     char szData[1024];
  91.  
  92.     // NULL out our data...
  93.     strcpy(szExt,"");
  94.     strcpy(szDesc, "");
  95.     strcpy(szEXEPathName, "");
  96.     iImage = ID_DOCUMENT;   // Image used to represent this file type in listbox.
  97.  
  98.     string strKeyword;
  99.  
  100.     strKeyword = "Type";
  101.  
  102.     // I.e., Type1 - Type20, etc.
  103.     char szTemp[20];
  104.     itoa(nIndex, szTemp, 10);
  105.     strKeyword += szTemp;
  106.  
  107.     HINI hini = PrfOpenProfile(WinQueryAnchorBlock(hwnd), lpszPRFFilename);
  108.  
  109.     if(hini == NULLHANDLE)
  110.         return FALSE;
  111.  
  112.     ULONG ulBytes = PrfQueryProfileString(hini,
  113.                                 lpszSection, strKeyword.c_str(), "",
  114.                                 szData, 1024);
  115.  
  116.     // (For Windows) GetPrivateProfileString(lpszSection, strKeyword.c_str(), "", szData, 1024, lpszPRFFilename);
  117.  
  118.     PrfCloseProfile(hini);
  119.  
  120.     if(ulBytes == 0 || strlen(szData)==0)
  121.         return FALSE;
  122.  
  123.     // Otherwise, parse out our fields.
  124.     LPSTR p;
  125.     p = strtok(szData, ",");
  126.  
  127.     // File extension
  128.     if(p)
  129.         strcpy(szExt, p);
  130.     else
  131.         return FALSE;
  132.  
  133.     p = strtok(NULL, ",");
  134.  
  135.     // File description
  136.     if(p)
  137.         strcpy(szDesc, p);
  138.     else
  139.         return FALSE;
  140.  
  141.     // .EXE path used for this document
  142.     p = strtok(NULL, ",");
  143.     if(p)
  144.         strcpy(szEXEPathName, p);
  145.     else
  146.         return FALSE;
  147.  
  148.     // Image index used for this document
  149.     p = strtok(NULL, ",");
  150.     if(p)
  151.         iImage = atoi(p);
  152.     else
  153.         iImage = ID_DOCUMENT;
  154.  
  155.     return TRUE;
  156. }
  157.  
  158.  
  159.